<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
		<script src="js/jquery-3.2.1.min.js" type="text/javascript" charset="utf-8"></script>
		<title>数组去重--支持一维和多维数组去重</title>
		<style type="text/css">
		</style>
	</head>
	<body>
		<div class="main">

		</div>
	</body>
	<script type="text/javascript">
		/**
		 * 数组去重--支持一维和多维数组去重
		 * @param {Array} arr   要去重的数组
		 * @return {Array}  返回去重的数组
		 */
		function uniqueArr(arr) {
			function formatArr(arr) {
				return arr.reduce((prev, current) => {
					if (!prev.some(item => equal(item, current))) {
						prev.push(current)
					}
					return prev
				}, [])
			}

			function equal(data, data1) {
				if (typeof data !== typeof data1) return false;
				if (typeof data !== 'object') {
					return data === data1
				}
				return Object.entries(data).every(([key, value]) => {
					if (typeof value === 'object') {
						if (typeof data1[key] !== 'object') {
							return false
						}
						return equal(value, data1[key])
					}
					return value === data1[key]
				})
			};
			return formatArr(arr)
		}

		let arr = [123, "meili", "123", "mogu", 123]
		// let  arr=[123, [1, 2, 3], [1, "2", 3], [1, 2, 3], "meili"];
		// let  arr=[123, {a: 1}, {a: {b: 1,c:1}}, {a: "1"}, {a: {c: 1, b: 1}}, "meili"];
		console.log(uniqueArr(arr))


		// 下面是常用的简单方法
		let easyArr = [1, 1, 3, 45, 5, 5, 7, 7];
		console.log(Array.from(new Set(easyArr)));
		console.log([...new Set(easyArr)])
		
	</script>
</html>
